package com.kgc.itrip.service.impl;

import com.kgc.itrip.common.*;
import com.kgc.itrip.component.RedisTools;
import com.kgc.itrip.dto.Dto;
import com.kgc.itrip.mapper.ItripUserMapper;
import com.kgc.itrip.pojo.ItripUser;
import com.kgc.itrip.response.ResultCode;
import com.kgc.itrip.service.ItripUserService;
import com.kgc.itrip.service.TokenService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItripUserServiceImpl implements ItripUserService {

    @Resource
    private TokenService tokenService;
    @Resource
    private ItripUserMapper itripUserMapper;

    @Resource
    private RedisTools redisTools;

    public ItripUser getItripUserById(Long id)throws Exception{
        return itripUserMapper.getItripUserById(id);
    }

    public List<ItripUser>	getItripUserListByMap(Map<String,Object> param)throws Exception{
        return itripUserMapper.getItripUserListByMap(param);
    }

    public Integer getItripUserCountByMap(Map<String,Object> param)throws Exception{
        return itripUserMapper.getItripUserCountByMap(param);
    }

    public Integer itriptxAddItripUser(ItripUser itripUser)throws Exception{
        itripUser.setActivated(Constants.USER_STATUS.NO_ACTIVED);
        //将用户的密码进行MD5加密
        itripUser.setUserPassword(MD5.getMd5(itripUser.getUserPassword(),32));
        itripUser.setCreationDate(new Date());
        return itripUserMapper.insertItripUser(itripUser);
    }

    public Integer itriptxModifyItripUser(ItripUser itripUser)throws Exception{
        itripUser.setModifyDate(new Date());
        return itripUserMapper.updateItripUser(itripUser);
    }

    public Integer itriptxDeleteItripUserById(Long id)throws Exception{
        return itripUserMapper.deleteItripUserById(id);
    }

    public Page<ItripUser> queryItripUserPageByMap(Map<String,Object> param, Integer pageNo, Integer pageSize)throws Exception{
        Integer total = itripUserMapper.getItripUserCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<ItripUser> itripUserList = itripUserMapper.getItripUserListByMap(param);
        page.setRows(itripUserList);
        return page;
    }

    //根据用户名判断是否已经注册过了
    @Override
    public Dto isExisitByName(String name) throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("userCode",name);
        List<ItripUser> userList = this.getItripUserListByMap(params);
        if(userList!=null && userList.size()>0){
            return DtoUtil.returnFail(ResultCode.USER_IS_EXISTS.getCode(),ResultCode.USER_IS_EXISTS.getMsg());
        }
        return DtoUtil.returnSuccess();
    }

    @Override
    public Dto itriptxActiveAccount(String user, String code) throws Exception {
        //1.根据手机号码去数据库中验证手机号码是否正确
        Map<String,Object> params = new HashMap<>();
        params.put("userCode",user);
        params.put("activated",Constants.USER_STATUS.NO_ACTIVED);
        List<ItripUser> users = getItripUserListByMap(params);
        if(users==null || users.size()==0){//说明没有查询到相应的用户
            return DtoUtil.returnFail(ResultCode.NOT_DATA_FOUNT.getCode(),"电话号码输入不正确");
        }
        //1.user去redis中查询验证码
        String redisCode = redisTools.getObject(Constants.ACCOUNT_ACTIVE_PREFIX.SMS_PREFIX + user,
                String.class);
        //判断验证码，如果返回值为
        if(EmptyUtils.isEmpty(redisCode)){
            return DtoUtil.returnFail(ResultCode.CODE_IS_TIMEOUT.getCode(),
                    ResultCode.CODE_IS_TIMEOUT.getMsg());
        }
        //判断验证码是否正确
        if(!redisCode.equals(code)){
            return DtoUtil.returnFail(ResultCode.CODE_IS_ERROR.getCode(),
                    ResultCode.CODE_IS_ERROR.getMsg());
        }
        //修改账户的激活状态
        ItripUser itripUser = users.get(0);
        itripUser.setActivated(Constants.USER_STATUS.ACTIVED);//设置为激活状态
        itripUser.setModifyDate(new Date());
        itripUserMapper.updateItripUser(itripUser);
        return DtoUtil.returnSuccess("账户激活成功");
    }

    @Override
    public Dto itriptxActiveEmail(String user, String code) throws Exception {
        //1.根据邮箱号码去数据库中验证邮箱号号码是否正确
        Map<String,Object> params = new HashMap<>();
        params.put("userCode",user);
        params.put("activated",Constants.USER_STATUS.NO_ACTIVED);
        List<ItripUser> users = getItripUserListByMap(params);
        if(users==null || users.size()==0){//说明没有查询到相应的用户
            return DtoUtil.returnFail(ResultCode.NOT_DATA_FOUNT.getCode(),"邮箱号码输入不正确");
        }
        //1.user去redis中查询验证码
        String redisCode = redisTools.getObject(Constants.ACCOUNT_ACTIVE_PREFIX.EMAIL_PREFIX + user, String.class);
        //判断验证码，如果返回值为
        if(EmptyUtils.isEmpty(redisCode)){
            return DtoUtil.returnFail(ResultCode.CODE_IS_TIMEOUT.getCode(),
                    ResultCode.CODE_IS_TIMEOUT.getMsg());
        }
        //判断验证码是否正确
        if(!redisCode.equals(code)){
            return DtoUtil.returnFail(ResultCode.CODE_IS_ERROR.getCode(),
                    ResultCode.CODE_IS_ERROR.getMsg());
        }
        //修改账户的激活状态
        ItripUser itripUser = users.get(0);
        itripUser.setActivated(Constants.USER_STATUS.ACTIVED);//设置为激活状态
        int o = itripUser.getActivated();
        System.out.println(o);
        itripUser.setModifyDate(new Date());
        itripUserMapper.updateItripUser(itripUser);
        return DtoUtil.returnSuccess("账户激活成功");
    }

    @Override
    public Dto logOut(String agent,String token) throws Exception {
        if(!tokenService.validate(agent,token)){
            return DtoUtil.returnFail(ResultCode.NO_LOGIN.getMsg(),
                    ResultCode.NO_LOGIN.getCode());
        }
        tokenService.delete(token);
        return DtoUtil.returnSuccess("登出成功");
    }

}
